package com.oss.robot;

import java.util.List;

import com.oss.model.Task;

/**
 * @author xueqiang.mi
 * @since 2010-7-13
 */
public abstract class AbstractRetrieveRobot extends BaseRobot implements RetrieveRobot {

  public abstract void extractData(Task task) throws Exception;

  public abstract String getSource();

  public abstract Integer getTasksNumber();

  @Override
  public void prepareTasks() {
    try {
      List<Task> tasks = retrieveLinks();
      if (tasks != null) {
        taskService.addTasks(tasks);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @Override
  public void retrieveData() {
    // get task with specified number
    List<Task> tasks = taskService.getTasks(getTasksNumber(), getSource());
    // execute tasks
    for (Task task : tasks) {
      try {
        extractData(task);
        taskService.updateStatus(task.getId());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  public abstract List<Task> retrieveLinks() throws Exception;

}
